from qturboframe.lib.webdriver.data_change import deal_time
from qturboframe.lib.report.log import logger
from qturboframe.lib.utils.check import CheckBasic

from .base_element import BaseElement


class Timer(BaseElement, CheckBasic):
    '''
    Jres2.0 时间控件

    :Attributes:
     - _driver: 控件所在的当前webdriver对象
     - _element: 控件元素webelement对象
     - timeout: 步骤操作的超时时间

    :Example:
     - 例1: Timer(webelement)
     - 例2: Timer({By.NAME:'name'})
     - 例3: Timer({By.CLASS_NAME:'class_name','text':'文本信息'})
     - 例4: Timer({By.CLASS_NAME:'class_name','text':'文本信息'}, father=driver)
     - 例5: Timer({By.CLASS_NAME:'class_name','text':'文本信息'}, father=webelement)
    '''

    def __init__(self, element_para, father=None, timeout=5):
        super().__init__(element_para, father, timeout)

    def set_value(self, div, value):
        """
        jres2.0的选择时间

        :Args:
         - div: 时间控件
         - value: 时间值

        :Example:
         - 例: timer.set_value("112434")
        """
        value = deal_time(value)
        valuelist = value.split(':')
        
        locator = 'h-time-picker-cells-ul'
        ul_list = self.element.find_elements_by_attribute('class name', locator,timeout=self.timeout)
        for ul in ul_list:
            ul_class = ul.get_attribute('class')
            if 'hours' in ul_class:
                self.__set_time(ul, valuelist[0])
            elif 'minute' in ul_class:
                self.__set_time(ul, valuelist[1])
            elif 'seconds' in ul_class:
                self.__set_time(ul, valuelist[2])
            else:
                continue

    def __set_time(self, ul, text):
        ul.find_element_by_attribute('tag name', 'li', 'text:' + text, timeout=self.timeout).secure_click()

    def is_default(self, value):
        """
        检查jres2.0的表单控件默认值

        :Args:
         - inputele: 表单控件
         - value: 待检查的值

        :Returns:
            检查匹配返回True，否则返回False

        :Example:
         - 例: timer.is_default()
        """
        if self._element.text:
            text = self._element.text
        elif self._element.get_attribute('defaultvalue'):
            text = self._element.get_attribute('defaultvalue')
        elif self._element.get_attribute('value'):
            text = self._element.get_attribute('value')
        elif self._element.get_attribute('placeholder'):
            text = self._element.get_attribute('placeholder')
        elif self._element.get_attribute('title'):
            text = self._element.get_attribute('title')
        else:
            text = ""
        if text == value:
            return True
        else:
            logger.error('多行文本默认值与校验值不一致！')
            return False